<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    在Python中使用httpx模块.md |  
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  

</head>

</html>

<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-develop/在Python中使用httpx模块"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  在Python中使用httpx模块.md
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/11/11/develop/%E5%9C%A8Python%E4%B8%AD%E4%BD%BF%E7%94%A8httpx%E6%A8%A1%E5%9D%97/" class="article-date">
  <time datetime="2020-11-10T16:00:00.000Z" itemprop="datePublished">2020-11-11</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/develop/">develop</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">2.5k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">13 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h1 id="在Python中使用httpx模块"><a href="#在Python中使用httpx模块" class="headerlink" title="在Python中使用httpx模块"></a>在Python中使用httpx模块</h1><h3 id="导入httpx"><a href="#导入httpx" class="headerlink" title="导入httpx"></a>导入<code>httpx</code></h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">In [25]: import httpx  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>获取一个网页</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">In [26]: r &#x3D; httpx.get(&quot;https:&#x2F;&#x2F;httpbin.org&#x2F;get&quot;)  </span><br><span class="line">  </span><br><span class="line">In [27]: r  </span><br><span class="line">Out[27]: &lt;Response [200 OK]&gt;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>同样,发送<code>HTTP POST</code>请求:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">In [28]: r &#x3D; httpx.post(&quot;https:&#x2F;&#x2F;httpbin.org&#x2F;post&quot;, data&#x3D;&#123;&quot;key&quot;: &quot;value&quot;&#125;)  </span><br><span class="line">  </span><br><span class="line">In [29]: r  </span><br><span class="line">Out[29]: &lt;Response [200 OK]&gt;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>PUT,DELETE,HEAD和OPTIONS请求都遵循相同的方式:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">In [35]: r &#x3D; httpx.put(&quot;https:&#x2F;&#x2F;httpbin.org&#x2F;put&quot;, data&#x3D;&#123;&quot;key&quot;: &quot;value&quot;&#125;)  </span><br><span class="line">  </span><br><span class="line">In [36]: r &#x3D; httpx.delete(&quot;https:&#x2F;&#x2F;httpbin.org&#x2F;delete&quot;)  </span><br><span class="line">  </span><br><span class="line">In [37]: r &#x3D; httpx.head(&quot;https:&#x2F;&#x2F;httpbin.org&#x2F;get&quot;)  </span><br><span class="line">  </span><br><span class="line">In [38]: r &#x3D; httpx.options(&quot;https:&#x2F;&#x2F;httpbin.org&#x2F;get&quot;)  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="在URL中传递参数"><a href="#在URL中传递参数" class="headerlink" title="在URL中传递参数"></a>在<code>URL</code>中传递参数</h3><ol>
<li>在请求URL中传递查询参数,请使用<code>params</code>关键字</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">In [41]: r &#x3D; httpx.get(&quot;https:&#x2F;&#x2F;httpbin.org&#x2F;get&quot;, params&#x3D;params)  </span><br><span class="line">  </span><br><span class="line">In [42]: r  </span><br><span class="line">Out[42]: &lt;Response [200 OK]&gt;  </span><br><span class="line">  </span><br><span class="line"># 检查发出请求结果的&#96;URL&#96;  </span><br><span class="line">In [43]: r.url  </span><br><span class="line">Out[43]: URL(&#39;https:&#x2F;&#x2F;httpbin.org&#x2F;get?key1&#x3D;value2&amp;key2&#x3D;value2&#39;)  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ol start="2">
<li>将项目列表作为值传递</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">In [44]: params &#x3D; &#123;&quot;key1&quot;: &quot;value2&quot;, &quot;key2&quot;: [&quot;value2&quot;, &quot;value3&quot;]&#125;  </span><br><span class="line">  </span><br><span class="line">In [45]: r &#x3D; httpx.get(&quot;https:&#x2F;&#x2F;httpbin.org&#x2F;get&quot;, params&#x3D;params)  </span><br><span class="line">  </span><br><span class="line">In [46]: r.url  </span><br><span class="line">Out[46]: URL(&#39;https:&#x2F;&#x2F;httpbin.org&#x2F;get?key1&#x3D;value2&amp;key2&#x3D;value2&amp;key2&#x3D;value3&#39;)  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="响应内容"><a href="#响应内容" class="headerlink" title="响应内容"></a>响应内容</h3><p>HTTPX将自动处理响应内容解码为<code>Unicode</code>文本</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">In [52]: r &#x3D; httpx.get(&quot;https:&#x2F;&#x2F;www.example.org&#x2F;&quot;)  </span><br><span class="line">  </span><br><span class="line">In [53]: r.text  </span><br><span class="line">Out[53]: &#39;&lt;!doctype html&gt;\n&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;Example Domain&lt;&#x2F;title&gt;\n\n    &lt;meta charset&#x3D;&quot;utf-8&quot; &#x2F;&gt;\n    &lt;meta http-equiv&#x3D;&quot;Content-type&quot; content&#x3D;&quot;text&#x2F;html; charset&#x3D;utf-8&quot; &#x2F;&gt;\n    &lt;meta  &#x2F;&gt;\n    &lt;style type&#x3D;&quot;text&#x2F;css&quot;&gt;\n    body &#123;\n        background-color: #f0f0f2;\n        margin: 0;\n        padding: 0;\n        font-family: -apple-system, system-ui, BlinkMacSystemFont, &quot;Segoe UI&quot;, &quot;Open Sans&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;\n        \n    &#125;\n    div &#123;\n        width: 600px;\n        margin: 5em auto;\n        padding: 2em;\n        background-color: #fdfdff;\n        border-radius: 0.5em;\n        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);\n    &#125;\n    a:link, a:visited &#123;\n        color: #38488f;\n        text-decoration: none;\n    &#125;\n    @media (max-width: 700px) &#123;\n        div &#123;\n            margin: 0 auto;\n            width: auto;\n        &#125;\n    &#125;\n    &lt;&#x2F;style&gt;    \n&lt;&#x2F;head&gt;\n\n&lt;body&gt;\n&lt;div&gt;\n    &lt;h1&gt;Example Domain&lt;&#x2F;h1&gt;\n    &lt;p&gt;This domain is for use in illustrative examples in documents. You may use this\n    domain in literature without prior coordination or asking for permission.&lt;&#x2F;p&gt;\n    &lt;p&gt;&lt;a href&#x3D;&quot;https:&#x2F;&#x2F;www.iana.org&#x2F;domains&#x2F;example&quot;&gt;More information...&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;\n&lt;&#x2F;div&gt;\n&lt;&#x2F;body&gt;\n&lt;&#x2F;html&gt;\n&#39;  </span><br><span class="line">  </span><br><span class="line"># 解码  </span><br><span class="line">In [54]: r.encoding  </span><br><span class="line">Out[54]: &#39;UTF-8&#39;  </span><br><span class="line">  </span><br><span class="line"># 设置需要使用的编码  </span><br><span class="line">In [55]: r.encoding &#x3D; &quot;ISO-8859-1&quot;  </span><br><span class="line">  </span><br><span class="line">In [56]: print(r.headers, r.http_version, r.url, r.status_code)  </span><br><span class="line">Headers(&#123;&#39;content-encoding&#39;: &#39;gzip&#39;, &#39;age&#39;: &#39;532553&#39;, &#39;cache-control&#39;: &#39;max-age&#x3D;604800&#39;, &#39;content-type&#39;: &#39;text&#x2F;html; charset&#x3D;UTF-8&#39;, &#39;date&#39;: &#39;Sat, 06 Jun 2020 04:22:26 GMT&#39;, &#39;etag&#39;: &#39;&quot;3147526947+gzip&quot;&#39;, &#39;expires&#39;: &#39;Sat, 13 Jun 2020 04:22:26 GMT&#39;, &#39;last-modified&#39;: &#39;Thu, 17 Oct 2019 07:18:26 GMT&#39;, &#39;server&#39;: &#39;ECS (oxr&#x2F;830C)&#39;, &#39;vary&#39;: &#39;Accept-Encoding&#39;, &#39;x-cache&#39;: &#39;HIT&#39;, &#39;content-length&#39;: &#39;648&#39;&#125;) HTTP&#x2F;1.1 https:&#x2F;&#x2F;www.example.org&#x2F; 200  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="返回JSON响应内容"><a href="#返回JSON响应内容" class="headerlink" title="返回JSON响应内容"></a>返回JSON响应内容</h3><p>通常, Web API 响应将被编码为JSON</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">In [59]: r &#x3D; httpx.get(&quot;https:&#x2F;&#x2F;api.github.com&#x2F;events&quot;)  </span><br><span class="line">In [60]: r.json()  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="自定义header"><a href="#自定义header" class="headerlink" title="自定义header"></a>自定义<code>header</code></h3><p>要在传出请求中包含其他标头,请使用<code>header</code>关键字参数:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">In [60]: url &#x3D; &#39;http:&#x2F;&#x2F;httpbin.org&#x2F;headers&#39;  </span><br><span class="line">  </span><br><span class="line">In [61]: headers &#x3D; &#123;&quot;user-agent&quot;: &quot;my-app&#x2F;0.0.1&quot;&#125;  </span><br><span class="line">  </span><br><span class="line">In [62]: r &#x3D; httpx.get(url, headers&#x3D;headers)  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="发送表单数据"><a href="#发送表单数据" class="headerlink" title="发送表单数据"></a>发送表单数据</h3><p>某些类型的HTTP请求,如<code>POST|PUT</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">In [64]: data &#x3D; &#123;&quot;key1&quot;:&quot;value1&quot;, &quot;key1&quot;: &quot;value2&quot;&#125;  </span><br><span class="line">  </span><br><span class="line">In [65]: r &#x3D; httpx.post(&quot;https:&#x2F;&#x2F;httpbin.org&#x2F;post&quot;, data&#x3D;data)  </span><br><span class="line">  </span><br><span class="line">In [66]: print(r.text)  </span><br><span class="line">&#123;  </span><br><span class="line">  &quot;args&quot;: &#123;&#125;,  </span><br><span class="line">  &quot;data&quot;: &quot;&quot;,  </span><br><span class="line">  &quot;files&quot;: &#123;&#125;,  </span><br><span class="line">  &quot;form&quot;: &#123;  </span><br><span class="line">    &quot;key1&quot;: &quot;value2&quot;  </span><br><span class="line">  &#125;,  </span><br><span class="line">  &quot;headers&quot;: &#123;  </span><br><span class="line">    &quot;Accept&quot;: &quot;*&#x2F;*&quot;,  </span><br><span class="line">    &quot;Accept-Encoding&quot;: &quot;gzip, deflate&quot;,  </span><br><span class="line">    &quot;Content-Length&quot;: &quot;11&quot;,  </span><br><span class="line">    &quot;Content-Type&quot;: &quot;application&#x2F;x-www-form-urlencoded&quot;,  </span><br><span class="line">    &quot;Host&quot;: &quot;httpbin.org&quot;,  </span><br><span class="line">    &quot;User-Agent&quot;: &quot;python-httpx&#x2F;0.12.1&quot;,  </span><br><span class="line">    &quot;X-Amzn-Trace-Id&quot;: &quot;Root&#x3D;1-5edb1e61-86ac64e4f9f165244f4dbd68&quot;  </span><br><span class="line">  &#125;,  </span><br><span class="line">  &quot;json&quot;: null,  </span><br><span class="line">  &quot;origin&quot;: &quot;114.113.113.186&quot;,  </span><br><span class="line">  &quot;url&quot;: &quot;https:&#x2F;&#x2F;httpbin.org&#x2F;post&quot;  </span><br><span class="line">&#125;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>表单编码的数据还可以包括给定键的多个值:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">In [72]: data &#x3D; &#123;&quot;userList&quot;: [&quot;shuke&quot;, &quot;jack&quot;]&#125;  </span><br><span class="line">  </span><br><span class="line">In [73]: r &#x3D; httpx.post(&quot;https:&#x2F;&#x2F;httpbin.org&#x2F;post&quot;, data&#x3D;data)  </span><br><span class="line">  </span><br><span class="line">In [74]: print(r.text)  </span><br><span class="line">&#123;  </span><br><span class="line">  &quot;args&quot;: &#123;&#125;,  </span><br><span class="line">  &quot;data&quot;: &quot;&quot;,  </span><br><span class="line">  &quot;files&quot;: &#123;&#125;,  </span><br><span class="line">  &quot;form&quot;: &#123;  </span><br><span class="line">    &quot;userList&quot;: [  </span><br><span class="line">      &quot;shuke&quot;,  </span><br><span class="line">      &quot;jack&quot;  </span><br><span class="line">    ]  </span><br><span class="line">  &#125;,  </span><br><span class="line">  &quot;headers&quot;: &#123;  </span><br><span class="line">    &quot;Accept&quot;: &quot;*&#x2F;*&quot;,  </span><br><span class="line">    &quot;Accept-Encoding&quot;: &quot;gzip, deflate&quot;,  </span><br><span class="line">    &quot;Content-Length&quot;: &quot;28&quot;,  </span><br><span class="line">    &quot;Content-Type&quot;: &quot;application&#x2F;x-www-form-urlencoded&quot;,  </span><br><span class="line">    &quot;Host&quot;: &quot;httpbin.org&quot;,  </span><br><span class="line">    &quot;User-Agent&quot;: &quot;python-httpx&#x2F;0.12.1&quot;,  </span><br><span class="line">    &quot;X-Amzn-Trace-Id&quot;: &quot;Root&#x3D;1-5edb1fc2-33761faf662f0b832cb806bb&quot;  </span><br><span class="line">  &#125;,  </span><br><span class="line">  &quot;json&quot;: null,  </span><br><span class="line">  &quot;origin&quot;: &quot;114.113.113.186&quot;,  </span><br><span class="line">  &quot;url&quot;: &quot;https:&#x2F;&#x2F;httpbin.org&#x2F;post&quot;  </span><br><span class="line">&#125;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>发送分段文件上传</p>
<p>使用HTTP分段编码上传文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">In [76]: files &#x3D; &#123;&quot;upload-file&quot;: open(&quot;&#x2F;Users&#x2F;shuke&#x2F;Work&#x2F;pha&#x2F;docker-compose.yml&quot;, &quot;rb&quot;)&#125;  </span><br><span class="line">  </span><br><span class="line">In [77]: r &#x3D; httpx.post(&quot;https:&#x2F;&#x2F;httpbin.org&#x2F;post&quot;, files&#x3D;files)  </span><br><span class="line">  </span><br><span class="line">In [78]: print(r.text)  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>发送JSON编码数据</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">In [79]: data &#x3D; &#123;&quot;integer&quot;: 123, &quot;boolean&quot;: True, &quot;list&quot;: [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]&#125;  </span><br><span class="line">  </span><br><span class="line">In [80]: r &#x3D; httpx.post(&quot;https:&#x2F;&#x2F;httpbin.org&#x2F;post&quot;, json&#x3D;data)  </span><br><span class="line">  </span><br><span class="line">In [81]: print(r.text)  </span><br><span class="line">&#123;  </span><br><span class="line">  &quot;args&quot;: &#123;&#125;,  </span><br><span class="line">  &quot;data&quot;: &quot;&#123;\&quot;integer\&quot;: 123, \&quot;boolean\&quot;: true, \&quot;list\&quot;: [\&quot;a\&quot;, \&quot;b\&quot;, \&quot;c\&quot;]&#125;&quot;,  </span><br><span class="line">  &quot;files&quot;: &#123;&#125;,  </span><br><span class="line">  &quot;form&quot;: &#123;&#125;,  </span><br><span class="line">  &quot;headers&quot;: &#123;  </span><br><span class="line">    &quot;Accept&quot;: &quot;*&#x2F;*&quot;,  </span><br><span class="line">    &quot;Accept-Encoding&quot;: &quot;gzip, deflate&quot;,  </span><br><span class="line">    &quot;Content-Length&quot;: &quot;58&quot;,  </span><br><span class="line">    &quot;Content-Type&quot;: &quot;application&#x2F;json&quot;,  </span><br><span class="line">    &quot;Host&quot;: &quot;httpbin.org&quot;,  </span><br><span class="line">    &quot;User-Agent&quot;: &quot;python-httpx&#x2F;0.12.1&quot;,  </span><br><span class="line">    &quot;X-Amzn-Trace-Id&quot;: &quot;Root&#x3D;1-5edb217c-cd09ef8caa55514051957eb8&quot;  </span><br><span class="line">  &#125;,  </span><br><span class="line">  &quot;json&quot;: &#123;  </span><br><span class="line">    &quot;boolean&quot;: true,  </span><br><span class="line">    &quot;integer&quot;: 123,  </span><br><span class="line">    &quot;list&quot;: [  </span><br><span class="line">      &quot;a&quot;,  </span><br><span class="line">      &quot;b&quot;,  </span><br><span class="line">      &quot;c&quot;  </span><br><span class="line">    ]  </span><br><span class="line">  &#125;,  </span><br><span class="line">  &quot;origin&quot;: &quot;114.113.113.186&quot;,  </span><br><span class="line">  &quot;url&quot;: &quot;https:&#x2F;&#x2F;httpbin.org&#x2F;post&quot;  </span><br><span class="line">&#125;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="响应状态码"><a href="#响应状态码" class="headerlink" title="响应状态码"></a>响应状态码</h3><p>检查响应的<code>HTTP</code>状态码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">In [82]: r &#x3D; httpx.get(&quot;https:&#x2F;&#x2F;httpbin.org&#x2F;get&quot;)  </span><br><span class="line">  </span><br><span class="line">In [83]: r.status_code  </span><br><span class="line">Out[83]: 200  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>HTTPX还包括一个简单的快捷方式,用于通过其文本短语访问状态代码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">In [84]: r.status_code &#x3D;&#x3D; httpx.codes.OK  </span><br><span class="line">Out[84]: True  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>针对任何客户端或服务器错误响应(4xx或5xx状态代码) 引发异常:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">In [85]: not_found &#x3D; httpx.get(&quot;https:&#x2F;&#x2F;httpbin.org&#x2F;status&#x2F;404&quot;)  </span><br><span class="line">  </span><br><span class="line">In [86]: not_found.status_code  </span><br><span class="line">Out[86]: 404  </span><br><span class="line">  </span><br><span class="line">In [87]: not_found.rasise_for_status()  </span><br><span class="line">---------------------------------------------------------------------------  </span><br><span class="line">AttributeError                            Traceback (most recent call last)  </span><br><span class="line">&lt;ipython-input-87-c7b57e835640&gt; in &lt;module&gt;  </span><br><span class="line">----&gt; 1 not_found.rasise_for_status()  </span><br><span class="line">  </span><br><span class="line">AttributeError: &#39;Response&#39; object has no attribute &#39;rasise_for_status&#39;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="响应Headers"><a href="#响应Headers" class="headerlink" title="响应Headers"></a>响应Headers</h3><p>响应标头可作为类似于字典的接口使用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">In [88]: r.headers  </span><br><span class="line">Out[88]: Headers(&#123;&#39;date&#39;: &#39;Sat, 06 Jun 2020 04:56:56 GMT&#39;, &#39;content-type&#39;: &#39;application&#x2F;json&#39;, &#39;content-length&#39;: &#39;306&#39;, &#39;connection&#39;: &#39;keep-alive&#39;, &#39;server&#39;: &#39;gunicorn&#x2F;19.9.0&#39;, &#39;access-control-allow-origin&#39;: &#39;*&#39;, &#39;access-control-allow-credentials&#39;: &#39;true&#39;&#125;)  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="该Headers数据类型是不区分大小写的，所以你可以使用任何资本"><a href="#该Headers数据类型是不区分大小写的，所以你可以使用任何资本" class="headerlink" title="该Headers数据类型是不区分大小写的，所以你可以使用任何资本"></a>该<code>Headers</code>数据类型是不区分大小写的，所以你可以使用任何资本</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">In [89]: r.headers[&quot;Content-Type&quot;]  </span><br><span class="line">Out[89]: &#39;application&#x2F;json&#39;  </span><br><span class="line">  </span><br><span class="line">In [90]: r.headers.get(&quot;content-type&quot;)  </span><br><span class="line">Out[90]: &#39;application&#x2F;json&#39;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="流响应"><a href="#流响应" class="headerlink" title="流响应"></a>流响应</h3><blockquote>
<p>可以流式传输响应的二进制内容</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">In [91]: with httpx.stream(&quot;GET&quot;, &quot;https:&#x2F;&#x2F;www.example.com&quot;) as r:  </span><br><span class="line">    ...:     for data in r.iter_bytes():  </span><br><span class="line">    ...:         print(data)  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>或文字…</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">In [93]: with httpx.stream(&quot;GET&quot;, &quot;https:&#x2F;&#x2F;www.example.com&quot;) as r:  </span><br><span class="line">    ...:     for text in r.iter_text():  </span><br><span class="line">    ...:         print(text)  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>或逐行流文本…</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">In [96]: with httpx.stream(&quot;GET&quot;, &quot;https:&#x2F;&#x2F;www.example.com&quot;) as r:  </span><br><span class="line">    ...:     for text in r.iter_lines():  </span><br><span class="line">    ...:         print(text)  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="Cookies"><a href="#Cookies" class="headerlink" title="Cookies"></a>Cookies</h3><p>可以轻松访问响应中设置的任何cookie:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">In [97]: r &#x3D; httpx.get(&quot;http:&#x2F;&#x2F;httpbin.org&#x2F;cookies&#x2F;set?chocolate&#x3D;chip&quot;, allow_redirects&#x3D;False)  </span><br><span class="line">  </span><br><span class="line">In [98]: r.cookies[&quot;chocolate&quot;]  </span><br><span class="line">Out[98]: &#39;chip&#39;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>如果需要将<code>Cookies</code>包含在外发请求中,请使用<code>cookies</code>参数:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">In [99]: cookies &#x3D; &#123;&quot;peanut&quot;: &quot;butter&quot;&#125;  </span><br><span class="line">  </span><br><span class="line">In [100]: r &#x3D; httpx.get(&quot;http:&#x2F;&#x2F;httpbin.org&#x2F;cookies&quot;, cookies&#x3D;cookies)  </span><br><span class="line">  </span><br><span class="line">In [101]: r.json()  </span><br><span class="line">Out[101]: &#123;&#39;cookies&#39;: &#123;&#39;peanut&#39;: &#39;butter&#39;&#125;&#125;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><code>Cookies</code> 按 域访问设置</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">In [102]: cookies &#x3D; httpx.Cookies()  </span><br><span class="line">  </span><br><span class="line">In [103]: cookies.set(&#39;cookie_on_domain&#39;, &#39;hello, there!&#39;, domain&#x3D;&#39;httpbin.org&#39;)  </span><br><span class="line">  </span><br><span class="line">In [104]: cookies.set(&#39;cookies_off_domain&#39;, &#39;nope&#39;, domain&#x3D;&quot;example.org&quot;)  </span><br><span class="line">  </span><br><span class="line">In [105]: r &#x3D; httpx.get(&quot;http:&#x2F;&#x2F;httpbin.org&#x2F;cookies&quot;, cookies&#x3D;cookies)  </span><br><span class="line">  </span><br><span class="line">In [106]: r.json()  </span><br><span class="line">Out[106]: &#123;&#39;cookies&#39;: &#123;&#39;cookie_on_domain&#39;: &#39;hello, there!&#39;&#125;&#125;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><code>URL</code> 重定向和历史 默认情况下,HTTPX将对重定向执行除<code>HEAD</code>请求之外的任何操作。<code>history</code>响应的属性可用于检查所有后续重定向,它包含遵循他们的顺序的所有重定向响应的列表<code>GITHUB</code>将所有<code>HTTP</code>请求重定向到<code>HTTPS</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">In [107]: r &#x3D; httpx.get(&quot;http:&#x2F;&#x2F;github.com&quot;)  </span><br><span class="line">  </span><br><span class="line">In [108]: r.url  </span><br><span class="line">Out[108]: URL(&#39;https:&#x2F;&#x2F;github.com&#39;)  </span><br><span class="line">  </span><br><span class="line">In [109]: r.status_code  </span><br><span class="line">Out[109]: 200  </span><br><span class="line">  </span><br><span class="line">In [110]: r.history  </span><br><span class="line">Out[110]: []  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>您可以使用allow_redirects参数修改默认的重定向处理:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">In [113]: r &#x3D; httpx.head(&#39;http:&#x2F;&#x2F;github.com&#x2F;&#39;, allow_redirects&#x3D;True)  </span><br><span class="line">  </span><br><span class="line">In [114]: r.url  </span><br><span class="line">Out[114]: URL(&#39;https:&#x2F;&#x2F;github.com&#x2F;&#39;)  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="认证方式"><a href="#认证方式" class="headerlink" title="认证方式"></a>认证方式</h3><ol>
<li>HTTPX支持基本和摘要HTTP身份验证</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">In [122]: httpx.get(&quot;https:&#x2F;&#x2F;example.com&quot;, auth&#x3D;(&quot;my_user&quot;, &quot;password123&quot;))  </span><br><span class="line">     ...:  </span><br><span class="line">Out[122]: &lt;Response [200 OK]&gt;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ol start="2">
<li>摘要式身份验证的凭据</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">In [123]: auth &#x3D; httpx.DigestAuth(&quot;my_user&quot;, &quot;password123&quot;)  </span><br><span class="line">  </span><br><span class="line">In [124]: httpx.get(&quot;https:&#x2F;&#x2F;example.com&quot;, auth&#x3D;auth)  </span><br><span class="line">Out[124]: &lt;Response [200 OK]&gt;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><img src=""></p>
 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2020/11/11/develop/%E5%9C%A8Python%E4%B8%AD%E4%BD%BF%E7%94%A8httpx%E6%A8%A1%E5%9D%97/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/develop/" rel="tag">develop</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2020/11/11/develop/%E5%8D%8F%E7%A8%8B%EF%BC%8C%E7%BA%BF%E7%A8%8B%EF%BC%8C%E8%BF%9B%E7%A8%8B%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%8C%E5%9C%A8Python%E4%B8%AD%E6%80%8E%E4%B9%88%E5%BA%94%E7%94%A8/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            协程，线程，进程是什么，在Python中怎么应用.md
          
        </div>
      </a>
    
    
      <a href="/2020/11/11/develop/%E5%9F%BA%E4%BA%8ESpring%20Cloud%E5%BE%AE%E6%9C%8D%E5%8A%A1%E5%8C%96%E5%BC%80%E5%8F%91%E5%B9%B3%E5%8F%B0Cloud-Platform%E5%AE%8C%E6%95%B4%E8%A7%A3%E6%9E%90/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">基于Spring Cloud微服务化开发平台Cloud-Platform完整解析.md</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "",
    app_key: "",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2020
        <i class="ri-heart-fill heart_icon"></i> TzWind
      </li>
    </ul>
    <ul>
      <li>
        
        
        
        由 <a href="https://hexo.io" target="_blank">Hexo</a> 强力驱动
        <span class="division">|</span>
        主题 - <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="Hexo"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" target="_blank" rel="noopener" href="http://www.baidu.com">百度</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/2019/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


    
  </div>
</body>

</html>